<?php
/**
 * Created by JetBrains PhpStorm.
 * User: CAIXUDONG
 * Date: 11-6-20
 * Time: 下午4:17
 */
 
class BaoBeiInfoModel extends Model{

    public function get($baobei_id){

        $sql = "select * from baobeiinfo where baobei_id = :baobei_id";

        return $this->db()->getRow($sql,array('baobei_id'=>$baobei_id));
    }

    public function getBaoBeiInfoList($curr_page,$per_page,$filter = array()){
        $where = ' 1=1 ';
        $bindarr = array();

        if(isset($filter['baobei_name'])){
            $where .= ' and baobei_name like :baobei_name';
            $bindarr['baobei_name'] = '%'.$filter['baobei_name'].'%';
        }

        if(isset($filter['baobei_source_id'])){
            $where .= ' and baobei_source_id = :baobei_source_id';
            $bindarr['baobei_source_id'] = $filter['baobei_source_id'];
        }

        if(isset($filter['baobei_type_id'])){
            $where .= ' and baobei_type_id = :baobei_type_id';
            $bindarr['baobei_type_id'] = $filter['baobei_type_id'];
        }

        $ret = $this->pager($curr_page, $per_page)
                ->ct('select count(*) from baobeiinfo where ' . $where , $bindarr)
                ->rs('select * from baobeiinfo where '.$where.' order by baobei_id asc',$bindarr);
        return $ret;
    }

    public function checkBaobeiinfo($baobei_source_id,$baobei_flag){
        $sql = "select baobei_id from baobeiinfo where baobei_source_id = :baobei_source_id and baobei_flag = :baobei_flag";

        $bindarr = array(
            'baobei_source_id'=>$baobei_source_id,
            'baobei_flag'=>$baobei_flag
        );

        return $this->db()->getRow($sql,$bindarr);
    }

    public function addBaobei($baobei_name,$baobei_content,$baobei_tags,$baobei_price,$baobei_treasurer_name,$baobei_type_id,$baobei_source_id,$baobei_flag,$baobei_source_from_url,$baobei_image_url,$theme_id,$brand_id,$user_id){


       $check_ret = $this->checkBaobeiinfo($baobei_source_id,$baobei_flag);
       if($check_ret){
           return 'baby already exist';
       }

       $sql = "insert into baobeiinfo(baobei_name,baobei_content,baobei_tags,baobei_price,baobei_treasurer_name,baobei_type_id,baobei_source_id,baobei_flag,baobei_source_from_url,baobei_image_url,theme_id,brand_id,baobei_add_ip,baobei_add_time,user_id) values(:baobei_name,:baobei_content,:baobei_tags,:baobei_price,:baobei_treasurer_name,:baobei_type_id,:baobei_source_type,:baobei_flag,:baobei_source_from_url,:baobei_image_url,:theme_id,:brand_id,:baobei_add_ip,now(),:user_id)";

               $bindarr = array(
                   'baobei_name'=>$baobei_name,
                   'baobei_content'=>!empty($baobei_content) ? $baobei_content : null,
                   'baobei_tags'=>$baobei_tags,
                   'baobei_price'=>$baobei_price,
                   'baobei_treasurer_name'=>$baobei_treasurer_name,
                   'baobei_type_id'=>$baobei_type_id,
                   'baobei_source_id'=>$baobei_source_id,
                   'baobei_flag'=>$baobei_flag,
                   'baobei_source_from_url'=>$baobei_source_from_url,
                   'baobei_image_url'=>$baobei_image_url,
                   'theme_id'=>!empty($theme_id) ? $theme_id : null,
                   'brand_id'=>!empty($brand_id) ? $brand_id : null,
                'baobei_add_ip'=>return_ip(),
                   'user_id'=>$user_id
               );
               $this->db()->execute($sql,$bindarr);
               return  $this->db()->lastInsertId();

   }

   public function delBaoBeiInfo($baobei_id){

       $rs = $this->db()->execute("DELETE FROM baobeiinfo WHERE baobei_id = :baobei_id",array('baobei_id'=>$baobei_id));

       return $rs->rowCount();
   }
}